﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace WebDiagnostics
{
    public class PageResponseRenderer : IDiagnosticsResponseRenderer
    {
        private readonly HttpContextBase _context;
        private readonly IEnumerable<DiagnoserDescriptor> _diagnosers;

        public PageResponseRenderer(HttpContextBase context, IEnumerable<DiagnoserDescriptor> diagnosers)
        {
            _context = context;
            _diagnosers = diagnosers;
        }

        public void Render(TextWriter writer)
        {
            var htmlWriter = new HtmlTextWriter(writer);

            htmlWriter.WriteLine("<!DOCTYPE html>");
            htmlWriter.WriteLine("<html>");

            htmlWriter.WriteLine("<head>");
            RenderHeader(htmlWriter);
            htmlWriter.WriteLine("</head>");

            htmlWriter.WriteLine("<body>");
            RenderBody(htmlWriter);
            htmlWriter.WriteLine("</body>");

            htmlWriter.WriteLine("</html>");
        }

        protected virtual void RenderHeader(HtmlTextWriter writer)
        {
            RenderJQueryInclude(writer);
            writer.RenderStylesheetInclude("https://github.com/nathansmith/960-Grid-System/raw/master/code/css/960.css");
            writer.RenderStylesheetInclude("/diagnostics.css");
        }

        protected static void RenderJQueryInclude(HtmlTextWriter writer)
        {
            writer.WriteLine(@"
            <script type='text/javascript' src='http://ajax.microsoft.com/ajax/jquery/jquery-1.4.1.min.js'></script>
            <script type='text/javascript'>
            if (typeof jQuery == 'undefined')
            {
                document.write(unescape(""%3Cscript src='/Scripts/jquery-1.4.1.min.js' type='text/javascript'%3E%3C/script%3E""));
            }
            </script>");
        }

        protected virtual void RenderBody(HtmlTextWriter writer)
        {
            writer.RenderBeginDiv("container_12");

            writer.RenderHeader(1, "Website Diagnostics");

            int index = 0;

            foreach (var diagnoser in _diagnosers.OrderBy(x => x.Priority))
            {
                index++;

                string alphaOrOmega = "alpha";
                if (index % 2 == 0)
                    alphaOrOmega = "omega";

                string gridClass;
                switch (diagnoser.Width)
                {
                    case RenderWidth.Full:
                        gridClass = "grid_12";
                        break;
                    default:
                        gridClass = "grid_6";
                        break;
                }

                var containerClass = string.Format("diagnosis-container {0} {1}", gridClass, alphaOrOmega);

                writer.RenderBeginDiv(containerClass);

                    writer.WriteLine(diagnoser.Layout);
                    writer.WriteLine("<script type='text/javascript'>");
                    writer.WriteLine(diagnoser.LoaderScript);
                    writer.WriteLine("</script>");

                writer.RenderEndTag();

                if (index % 2 == 0)
                {
                    writer.RenderBeginDiv("clear");
                    writer.RenderEndTag();
                }
            }
            
            writer.RenderEndTag();
        }
    }
}
